package cn.net.xyan.easy.webapp.base.service.convert;

import cn.net.xyan.easy.webapp.base.api.dto.QueryUserRsq;
import cn.net.xyan.easy.webapp.base.entity.Permission;
import cn.net.xyan.easy.webapp.base.entity.PermissionGroup;
import cn.net.xyan.easy.webapp.base.entity.SystemUser;
import cn.net.xyan.easy.webapp.base.service.IEntityResponseConvert;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created by zarra on 16/2/23.
 */
@Component
public class UserRspUserConvert implements IEntityResponseConvert<QueryUserRsq,SystemUser> {
    @Override
    public boolean needContext() {
        return true;
    }

    @Override
    public QueryUserRsq convert(SystemUser entity, Map<String, Object> context) {
        Boolean showPassword = false;
        if (context!=null){
            Boolean contextValue = (Boolean) context.get("showPassword");
            if (contextValue!= null)
                showPassword = contextValue;
        }

        QueryUserRsq rsp = new QueryUserRsq();

        rsp.setUsername(entity.getUsername());
        rsp.setRealName(entity.getRealName());
        rsp.setEmail(entity.getEmail());
        rsp.setStatus(entity.getStatus().getIndex());
        if (showPassword)
            rsp.setPassword(entity.getPassword());
        for (Permission permission:entity.getPermissions())
            rsp.getPermissions().add(permission.getPermission());
        for (PermissionGroup group : entity.getPermissionGroups())
            rsp.getGroups().add(group.getGroupName());

        return rsp;
    }
}
